package cn.bugstack.domain.activity.service;

import cn.bugstack.domain.activity.adapter.repository.IActivityRepository;
import cn.bugstack.domain.activity.model.entity.MarketProductEntity;
import cn.bugstack.domain.activity.model.entity.TrialBalanceEntity;
import cn.bugstack.domain.activity.model.entity.UserGroupBuyOrderDetailEntity;
import cn.bugstack.domain.activity.model.valobj.TeamStatisticVO;
import cn.bugstack.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import cn.bugstack.domain.activity.service.trial.factory.DefaultActivityStrategyFactory.DynamicContext;
import cn.bugstack.types.design.framework.tree.StrategyHandler;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author hsj
 * @date 2025/2/12 11:04
 */
@Service
public class IndexGroupBuyMarketServiceImpl implements IIndexGroupBuyMarketService {


	@Resource
	private DefaultActivityStrategyFactory defaultActivityStrategyFactory;

	@Resource
	private IActivityRepository repository;



	@Override
	public TrialBalanceEntity indexMarketTrial(MarketProductEntity marketProductEntity) throws Exception {
		StrategyHandler<MarketProductEntity, DynamicContext, TrialBalanceEntity> strategyHandler = defaultActivityStrategyFactory.strategyHandler();;
		return 	strategyHandler.apply(marketProductEntity,new DynamicContext());
	}

	@Override
	public List<UserGroupBuyOrderDetailEntity> queryInProgressUserGroupBuyOrderDetailList(Long activityId, String userId, Integer ownerCount, Integer randomCount) {
		List<UserGroupBuyOrderDetailEntity> unionAllList = new ArrayList<>();

		// 查询个人拼团数据
		if (0 != ownerCount) {
			List<UserGroupBuyOrderDetailEntity> ownerList = repository.queryInProgressUserGroupBuyOrderDetailListByOwner(activityId, userId, ownerCount);
			if (null != ownerList && !ownerList.isEmpty()){
				unionAllList.addAll(ownerList);
			}
		}

		// 查询其他非个人拼团
		if (0 != randomCount) {
			List<UserGroupBuyOrderDetailEntity> randomList = repository.queryInProgressUserGroupBuyOrderDetailListByRandom(activityId, userId, randomCount);
			if (null != randomList && !randomList.isEmpty()){
				unionAllList.addAll(randomList);
			}
		}

		return unionAllList;
	}

	@Override
	public TeamStatisticVO queryTeamStatisticByActivityId(Long activityId) {
		return repository.queryTeamStatisticByActivityId(activityId);
	}

}
